from pymongo import MongoClient
import statistics
client = MongoClient(port=27017)
db=client['tiktok']
collist=['carsoftiktok']
#for col in db.list_collection_names():
for col in collist:
    print(col)
    for obj in db[col].find():
        if 'var_sb_new' not in obj['video_feature']['editing'].keys() or 'var_sb_c_new' not in obj['video_feature']['editing'].keys() or 'var_vgg_new' not in obj['video_feature']['editing'].keys() or 'var_vgg_c_new' not in obj['video_feature']['editing'].keys():
            vsb=0
            vvgg=0
            vsb_c=0
            vvgg_c=0
            img_list=list(obj['video_feature']['img_embed_new'].keys())
            for img_num in img_list:
                vvgg+=statistics.variance(obj['video_feature']['img_embed_new'][img_num][:1000])
                vsb+=statistics.variance(obj['video_feature']['img_embed_new'][img_num][1000:3096])
            if len(img_list)>1:

                for i in range(1000):
                    tempv=[obj['video_feature']['img_embed_new'][imgnum][i] for imgnum in img_list]
                    vvgg_c+=statistics.variance(tempv)
                for i in range(1000,3096):
                    tempv=[obj['video_feature']['img_embed_new'][imgnum][i] for imgnum in img_list]
                    vsb_c+=statistics.variance(tempv)
                vsb=vsb/len(img_list)
                vvgg = vvgg / len(img_list)
                vsb_c=vsb_c/1000
                vvgg_c=vvgg_c/2096
            db[col].update_one({'_id':obj['_id']},{'$set':{'video_feature.editing.var_sb_new':vsb,'video_feature.editing.var_vgg_new':vvgg,'video_feature.editing.var_sb_c_new':vsb_c,'video_feature.editing.var_vgg_c_new':vvgg_c}})